package com.entrogy.controller;

import com.entrogy.bean.*;
import com.entrogy.service.SchoolBusService;
import com.entrogy.service.SchoolBusTeacherService;
import com.entrogy.service.SchoolBusLineService;
import com.entrogy.service.SchoolbuslinedetailService;
import com.entrogy.utils.ApiReturnUtil;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 许红松
 * @date 2019/4/23 0023 下午 1:52
 * 对学校校车信息操作的相关接口
 */
@RestController
@Transactional
@RequestMapping(value = "/schoolBus")
@Api(description = "对学校校车信息操作的相关接口")
public class SchoolBusController {

    @Autowired
    private SchoolBusService schoolBusService;//学校校车service接口
    @Autowired
    private SchoolBusTeacherService schoolBusTeacherService;//学校校车关联教师service接口
    @Autowired
    private SchoolBusLineService schoolbuslineService;//校车站点service接口
    @Autowired
    private SchoolbuslinedetailService schoolbuslinedetailService;//校车各站点详情service接口

    /**
     *   校车管理分页
     * @return List
     */
    @ApiOperation(value = "分页获取所有校车的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "学校ID",name = "schoolCode",dataType = "int",paramType = "query",required = false),
            @ApiImplicitParam(value = "校车名称",name = "schoolbus_name",dataType = "String",paramType = "query",required = false),
            @ApiImplicitParam(name="page",value = "当前页数",required = false,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "每页条数",name = "total",dataType = "int",paramType = "query",required = false)
    })
    @RequestMapping(value = "/getSchoolBusList",method = RequestMethod.POST)
//    @RequiresPermissions("schoolBus:list")
    public PageBean<Map<String,Object>> getSchoolBusList(Integer  currentPage, Integer  pageSize, Integer schoolCode, String schoolbus_name ){
        Map<String,Object> map=new HashMap<>();
        if(schoolCode!=null&& schoolCode>0){
            map.put("school_code",schoolCode);
        }
        if ( schoolbus_name!=null && !"".equals("schoolbus_name")){
            map.put("schoolbus_name",schoolbus_name);
        }
        int total2=schoolBusService.total(map);
        PageHelper.startPage(currentPage,pageSize);
        List<Map<String,Object>> list=schoolBusService.GetSchoolBusIdByT(map);
        return new PageBean<Map<String,Object>>().pageBeans(list,total2);
    }

    /**
     *   新增校车信息
     * @param schoolBus
     * @param school_code
     * @return
     */
    @ApiOperation(value = "新增校车")
    @RequestMapping(value = "/insertSchoolBus",method = RequestMethod.POST)
//    @RequiresPermissions("schoolBus:add")
    public Map<String,Object> insertSchoolBus(@ModelAttribute("schoolBus")SchoolBus schoolBus, @RequestParam(value="school_code") Integer school_code){
        int code = schoolBusService.insertSchoolBus(schoolBus);
        if(code == 1){
            return ApiReturnUtil.success("新增成功","");
        }
        return ApiReturnUtil.error("新增失败");
    }

    /**
     *   修改校车信息
     * @return
     */
    @ApiOperation(value = "修改校车信息")
    @RequestMapping(value = "/UpdateSchoolBus",method = RequestMethod.POST)
//    @RequiresPermissions("schoolBus:update")
    public Map<String,Object> UpdateSchoolBus(@ModelAttribute("schoolBus") SchoolBus schoolBus){
        Boolean t = schoolBusService.UpdateSchoolBus(schoolBus);
        if(t == true){
            return ApiReturnUtil.success("修改成功","");
        }
        return ApiReturnUtil.error("修改失败");
    }

    /**
     *   删除校车信息
     * @return
     */
    @ApiOperation(value = "删除校车信息")
    @RequestMapping(value = "/deleteSchoolBus",method = RequestMethod.POST)
//    @RequiresPermissions("schoolBus:delete")
    public Map<String,Object> deleteSchoolBus(Integer schoolCode,String schoolbus_id){
        if (schoolCode==null|| schoolCode==0){
            return ApiReturnUtil.error("您无操作权限！");
        }
        int  code= schoolBusService.deleteSchoolBus(schoolbus_id);
        if(code==1){
            return ApiReturnUtil.success("删除成功","");
        }
        return ApiReturnUtil.error("删除失败");
    }

    /**
     *  需求 ： 获取发车列表，需要上车的学生列表及其是否在大厅 刷脸
     *   根据线路信息 联动查询 站点信息 显示下车人数或上车人数
     */
    /*@RequestMapping("/")
    public String getStudentListBysId(@Param(value="uid")String uid,@Param(value="busId")String busId,@Param(value="teacherid")String teacherid){

         if (teacherid!=null && !"".equals(teacherid)){
             //根据teacherid 去教师关联表中查询跟班车辆Id
             SchoolBusTeacher schoolBusTeacher=schoolBusTeacherService.selectSchoolBusTeacherByid(teacherid);
             if (schoolBusTeacher!=null){
                 Map<String,Object>  map=new HashMap<>();
                 map.put("schoolbus_id",schoolBusTeacher.getBusid());
                 map.put("school_code",schoolBusTeacher.getSchool_code());
                 List<Map<String,Object>> list=schoolBusService.GetStudentsOfBus(map);
             }
         }


        return "1";
    }
*/

    /**
     *  校车接口  用于学生列表
     *
     * @param schoolCode
     *
     * @return
     */
    @ApiOperation(value = "校车接口，用于学生列表信息")
    @RequestMapping(value = "/getSchoolBusList1",method = RequestMethod.POST)
    public List<Map<String,Object>> getSchoolBusList1( Integer schoolCode){
        Map<String,Object> map=new HashMap<>();
        if(schoolCode!=null&& schoolCode>0){
            map.put("school_code",schoolCode);
        }
        List<Map<String,Object>> list=schoolBusService.GetSchoolBusIdByT(map);
        return list;
    }


    /**  校车站点信息分页
     * @param schoolbus_id 校车id
     * @param school_code   学校id
     * @return  分页实体
     */
    @ApiOperation(value = "校车站点信息接口，用于某一班车次站点信息")
    @RequestMapping(value = "/getSchoolBusSiteList",method = RequestMethod.POST)
    public PageBean<SchoolBusLine> getSchoolBusSiteList( Integer school_code,Integer schoolbus_id,String schoolbus_Sitename,Integer currentPage,Integer pageSize,String SchoolRouteName){
        Map<String,Object> map=new HashMap<>();
        if(school_code!=null&& school_code>0){
            map.put("school_code",school_code);
        }
        if(schoolbus_id!=null&& schoolbus_id>0){//校车id
            map.put("schoolbus_id",schoolbus_id);
        }
        if(schoolbus_Sitename!=null&& !"".equals(schoolbus_Sitename)){//站点名称
            map.put("schoolbus_Sitename",schoolbus_Sitename);
        }
        if(SchoolRouteName!=null&& !"".equals(SchoolRouteName)){//站点名称
            map.put("SchoolRouteName",SchoolRouteName);
        }

        int total2=schoolbuslineService.count(map);
        PageHelper.startPage(currentPage,pageSize);
        List<SchoolBusLine> list=schoolbuslineService.getSchoolBusLineList(map);
        return new PageBean<SchoolBusLine>().pageBeans(list,total2);

    }

    /**
     *   新增校车站点信息
     *
     * @param schoolBusLine
     * @return
     */
    @ApiOperation(value = "新增校车站点")
    @RequestMapping(value = "/insertSchoolBusSite",method = RequestMethod.POST)
    public Map<String,Object> insertSchoolBusSite(@ModelAttribute("schoolBusLine")SchoolBusLine schoolBusLine){

        int code = schoolbuslineService.addSchoolBusLine(schoolBusLine);
        if(code == 1){
            return ApiReturnUtil.success("新增成功","");
        }
        return ApiReturnUtil.error("新增失败");
    }

    /**
     *   修改校车站点信息
     * @return
     */
    @ApiOperation(value = "修改校车站点信息")
    @RequestMapping(value = "/UpdateSchoolBusLine",method = RequestMethod.POST)
    public Map<String,Object> UpdateSchoolBusLine(@ModelAttribute("schoolBusLine") SchoolBusLine schoolBusLine){
        int code= schoolbuslineService.updateBusLine(schoolBusLine);
        if(code == 1){
            return ApiReturnUtil.success("修改成功","");
        }
        return ApiReturnUtil.error("修改失败");
    }

    /**
     *   删除校车站点信息
     * @return
     */
    @ApiOperation(value = "删除校车站点信息")
    @RequestMapping(value = "/deleteSchoolBusSite",method = RequestMethod.POST)
    public Map<String,Object> deleteSchoolBusSite(@RequestParam("school_code")Integer school_code,@RequestParam("schoolbus_Siteid") int schoolbus_Siteid){
        if (school_code==null|| school_code==0){
            return ApiReturnUtil.error("您无操作权限！");
        }
        int  code= schoolbuslineService.deleteSchoolBusLine(schoolbus_Siteid);
        if(code==1){
            return ApiReturnUtil.success("删除成功","");
        }
        return ApiReturnUtil.error("删除失败");
    }


    /**  某班校车站点流水信息分页
     * @param schoolbus_id 校车id
     * @param schoolCode   学校id
     * @return  分页实体
     */
    @ApiOperation(value = "某班校车站点流水信息接口，用于某一班车次站点信息")
    @RequestMapping(value = "/getSchoolBuslineDetailList",method = RequestMethod.POST)
    public PageBean<SchoolBuslineDetail> getSchoolBuslineDetailList( Integer schoolCode,@RequestParam(value="schoolbus_id") Integer schoolbus_id,Integer Type,String upTime,String downTime,String schoolbus_Sitename,Integer currentPage,Integer pageSize){
        Map<String,Object> map=new HashMap<>();
        if(schoolbus_id!=null&& schoolbus_id>0){//校车id
            map.put("schoolbus_id",schoolbus_id);
        }
        if(schoolCode!=null&& schoolCode>0){
            map.put("school_code",schoolCode);
        }
        if(Type!=null&& Type>0){
            map.put("Type",Type);
        }
        if(schoolbus_Sitename!=null&& !"".equals(schoolbus_Sitename)){//站点名称
            map.put("schoolbus_Sitename",schoolbus_Sitename);
        }
        if(upTime!=null&& !"".equals(upTime)){
            map.put("upTime",upTime);
        }
        if(downTime!=null&& !"".equals(downTime)){
            map.put("downTime",downTime);
        }
        int total2=schoolbuslinedetailService.getTotal(map);
        PageHelper.startPage(currentPage,pageSize);
        List<SchoolBuslineDetail> list=schoolbuslinedetailService.getList(map);
        return new PageBean<SchoolBuslineDetail>().pageBeans(list,total2);

    }

    /**
     *   教师List分页
     * @return List
     */
    @ApiOperation(value = "分页获取所有教师List信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "学校ID",name = "schoolCode",dataType = "int",paramType = "query",required = false),
            @ApiImplicitParam(value = "教师姓名",name = "name",dataType = "String",paramType = "query",required = false),
            @ApiImplicitParam(name="page",value = "当前页数",required = false,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "每页条数",name = "total",dataType = "int",paramType = "query",required = false)
    })
    @RequestMapping(value = "/getUserListByList",method = RequestMethod.POST)
    public PageBean<Map<String,Object>> getUserListByList(Integer  currentPage, Integer  pageSize, Integer schoolCode, String name ){
        Map<String,Object> map=new HashMap<>();
        if(schoolCode!=null&& schoolCode>0){
            map.put("school_code",schoolCode);
        }
        if ( name!=null && !"".equals("name")){
            map.put("name",name);
        }
        int total2=schoolBusService.getUserListTotal(map);
        PageHelper.startPage(currentPage,pageSize);
        List<Map<String,Object>> list=schoolBusService.getUserListByT(map);
        return new PageBean<Map<String,Object>>().pageBeans(list,total2);
    }
}
